package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSysLicence
//@description: 创建SysLicence记录
//@param: sysLicence model.SysLicence
//@return: err error

func CreateSysLicence(sysLicence model.SysLicence) (err error) {
	err = global.GVA_DB.Create(&sysLicence).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysLicence
//@description: 删除SysLicence记录
//@param: sysLicence model.SysLicence
//@return: err error

func DeleteSysLicence(sysLicence model.SysLicence) (err error) {
	err = global.GVA_DB.Delete(&sysLicence).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysLicenceByIds
//@description: 批量删除SysLicence记录
//@param: ids request.IdsReq
//@return: err error

func DeleteSysLicenceByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.SysLicence{}, "id in ?", ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateSysLicence
//@description: 更新SysLicence记录
//@param: sysLicence *model.SysLicence
//@return: err error

func UpdateSysLicence(sysLicence model.SysLicence) (err error) {
	err = global.GVA_DB.Save(&sysLicence).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysLicence
//@description: 根据id获取SysLicence记录
//@param: id uint
//@return: err error, sysLicence model.SysLicence

func GetSysLicence(id uint) (err error, sysLicence model.SysLicence) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysLicence).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetSysLicenceInfoList
//@description: 分页获取SysLicence记录
//@param: info request.SysLicenceSearch
//@return: err error, list interface{}, total int64

func GetSysLicenceInfoList(info request.SysLicenceSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.SysLicence{})
	var sysLicences []model.SysLicence
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.LicenceUUID != "" {
		db = db.Where("`licence_uuid` LIKE ?", "%"+info.LicenceUUID+"%")
	}
	if info.LicenceDuration != 0 {
		db = db.Where("`licence_duration` = ?", info.LicenceDuration)
	}
	if info.LicencePoints != 0 {
		db = db.Where("`licence_points` = ?", info.LicencePoints)
	}
	if info.Company != "" {
		db = db.Where("`company` LIKE ?", "%"+info.Company+"%")
	}
	err = db.Count(&total).Error
	err = db.Order("created_at DESC").Limit(limit).Offset(offset).Find(&sysLicences).Error
	return err, sysLicences, total
}
